﻿using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using NET6.WebAPI.Extensions;
using NET6.WebAPI.Models.Tool;
using SqlSugar;
using System.Security.Claims;

namespace NET6.WebAPI.Controllers.Base
{
    public class BusinessController : ControllerBase
    {
        /// <summary>
        /// 数据库访问客户端
        /// </summary>
        protected SqlSugarClient _db { get; }

        /// <summary>
        /// 对象映射器
        /// </summary>
        protected IMapper _mapper { get; }

        /// <summary>
        /// 当前用户
        /// </summary>
        protected LoginUser CurrentUser
        {
            get
            {
                if (User.Identity != null && User.Identity.IsAuthenticated)
                {
                    var user = new LoginUser
                    {
                        ID = (User.Claims.FirstOrDefault(it => it.Type == ClaimTypes.NameIdentifier)?.Value).ToInt(),
                        Name = User.Claims.FirstOrDefault(it => it.Type == ClaimTypes.Name)?.Value,
                        Gender = User.Claims.FirstOrDefault(it => it.Type == ClaimTypes.Gender)?.Value
                    };

                    return user;
                }

                return null;
            }
        }

        public BusinessController(SqlSugarClient db, IMapper mapper)
        {
            _db = db;
            _mapper = mapper;
        }
    }
}
